package com.luoye.restful;

import com.luoye.restful.model.*;
import lombok.Data;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

@Data
public class PackageScanner {
    private String scanPackage;
    private String prePath="";

    public List<ApiGroup> scan(){
        List<ApiGroup> apiGroupList=new ArrayList<ApiGroup>();

        ClassFinder classFinder=new ClassFinder();
        List<String> classList=classFinder.findAllClass(scanPackage);

        for(String className:classList){
            try {
                Class clazz=Class.forName(className);
                if(clazz.isAnnotationPresent(com.luoye.restful.annotation.ApiGroup.class)){
                    com.luoye.restful.annotation.ApiGroup aG = (com.luoye.restful.annotation.ApiGroup) clazz.getAnnotation(com.luoye.restful.annotation.ApiGroup.class);
                    ApiGroup apiGroup=new ApiGroup();
                    String gPath= prePath+aG.path();
                    apiGroup.setApiGroupTitle(aG.value());
                    apiGroup.setApiGroupDes(aG.description());
                    Method[] methods=clazz.getMethods();
                    for (Method method:methods){
                        if(method.isAnnotationPresent(com.luoye.restful.annotation.Api.class)){
                            com.luoye.restful.annotation.Api a=method.getAnnotation(com.luoye.restful.annotation.Api.class);
                            Api api=new Api();
                            api.setApiDes(a.description());
                            api.setApiMethod(a.method());
                            api.setApiTitle(a.value());
                            api.setApiUrl(gPath+a.path());
                            api.setRequestData(new ApiData(a.requestDataFormat(),new ArrayList<ApiDataDetail>()));
                            api.setResponseData(new ApiData(a.responseDataFormat(),new ArrayList<ApiDataDetail>()));
                            //获取参数
                            String[] paramNames=a.params();
                            Class[] paramTypes = method.getParameterTypes();
                            int i=0;
                            for(Class pt:paramTypes){
                                //获取实体名称和类型
                                ApiDataDetail apiDataDetail=getDataDetail(pt);
                                if(i<paramNames.length){
                                    apiDataDetail.setEntityName(paramNames[i]);
                                }
                                api.getRequestData().getDataDetailList().add(apiDataDetail);
                                i++;
                            }

                            //获取返回类型
                            Class<?> returnType =method.getReturnType();
                            //获取实体名称和类型
                            ApiDataDetail apiDataDetail=getDataDetail(returnType);
                            api.getResponseData().getDataDetailList().add(apiDataDetail);
                            apiGroup.getApiList().add(api);
                        }
                    }

                    System.out.println("apiGroup->"+apiGroup);
                    apiGroupList.add(apiGroup);
                }
            } catch (ClassNotFoundException e) {
            }
        }


        return apiGroupList;
    }


    private ApiDataDetail getDataDetail(Class pt){
        ApiDataDetail apiDataDetail=new ApiDataDetail();
        apiDataDetail.setEntityType(pt.getName());
        if(pt.isAnnotationPresent(com.luoye.restful.annotation.ApiData.class)){
            com.luoye.restful.annotation.ApiData ad= (com.luoye.restful.annotation.ApiData) pt.getAnnotation(com.luoye.restful.annotation.ApiData.class);
            //apiDataDetail.setEntityName(ad.value());
            Field[] fields= pt.getDeclaredFields();
            System.out.println("fields->"+fields.length);
            for(Field field:fields){
                System.out.println("field->"+field.getName());
                if(field.isAnnotationPresent(com.luoye.restful.annotation.ApiDataField.class)){
                    System.out.println("field->"+field.getName());
                    com.luoye.restful.annotation.ApiDataField adf=field.getAnnotation(com.luoye.restful.annotation.ApiDataField.class);
                    ApiDataField apiDataField=new ApiDataField();
                    apiDataField.setFieldDes(adf.fieldDes());
                    apiDataField.setFieldMust(adf.fieldMust());
                    apiDataField.setFieldName(adf.value());
                    //apiDataField.setFieldType(adf.fieldType());
                    apiDataField.setFieldType(field.getType().getName());
                    apiDataDetail.getApiDataFieldList().add(apiDataField);
                }
            }
        }
        System.out.println("apiDataDetail->"+apiDataDetail);
        return apiDataDetail;
    }
}
